package com.gdth.bank.model;

import android.database.Cursor;
import android.provider.ContactsContract;
import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.gdth.bank.BankApplication;
import com.gdth.bank.utils.AppUtil;
import com.gdth.database.bean.DaoSession;
import com.gdth.database.bean.TAppBfrjJbxxb;
import com.gdth.database.bean.TAppBfrjJbxxbDao;
import com.gdth.database.bean.TAppBfrjJtcyb;
import com.gdth.database.bean.TAppBfrjJtcybDao;
import com.gdth.http.method.HttpMethodServlet;
import com.gdth.utils.DateTimeUtil;

import org.greenrobot.greendao.DaoException;
import org.greenrobot.greendao.query.QueryBuilder;
import org.greenrobot.greendao.query.WhereCondition;

import rx.Observable;
import rx.Subscriber;

public class CjkhjlModel {

	public Observable<List<TAppBfrjJbxxb>> getqureAllCjkhxx(String zh, String value, Date date){
		final String val = value;
		final Date mDate = date;
		final String mZh = zh;
		return Observable.create(new Observable.OnSubscribe<List<TAppBfrjJbxxb>>() {

			@Override
			public void call(Subscriber<? super List<TAppBfrjJbxxb>> arg0) {
				// TODO Auto-generated method stub
				TAppBfrjJbxxbDao dao = BankApplication.getDaoSession().getTAppBfrjJbxxbDao();
//				QueryBuilder<TAppBfrjJbxxb> qb = dao.queryBuilder();
				TAppBfrjJtcybDao dao1 = BankApplication.getDaoSession().getTAppBfrjJtcybDao();
//				Date sDate = DateTimeUtil.strToDate("2016-01-01 00:00:00");
//				QueryBuilder<TAppBfrjJtcyb> qd1 = dao1.queryBuilder();
//				Date eDate = new Date(mDate.getTime() - 1000);
				String sqlStr = "SELECT DISTINCT b.* FROM "+
						dao.getTablename() +" b left join " + dao1.getTablename() +" j on b.uuid = j.jcuuid where (j.CYXM like "
						+ "'%" + val +"%' or j.ZJHM like '%"+val+"%')and j.SJZT != 0 and b.SJZT != 0 and b.CJRSFZ = '"+ zh +"' order by b.CJSJ DESC";
				List<TAppBfrjJbxxb> mList = new ArrayList<>();
				Cursor cursor = null;
				try{
					cursor = BankApplication.getDaoSession().getDatabase().rawQuery(sqlStr,null);
					if(cursor != null && cursor.moveToFirst())  {
						do {
							TAppBfrjJbxxb jbxxb = new TAppBfrjJbxxb();
							jbxxb.setId(cursor.getLong(cursor.getColumnIndex("_id")));
							jbxxb.setKhxm(cursor.getString(cursor.getColumnIndex("KHXM")));
							jbxxb.setZjlb(cursor.getInt(cursor.getColumnIndex("ZJLB")));
							jbxxb.setZjhm(cursor.getString(cursor.getColumnIndex("ZJHM")));
							jbxxb.setCjr(cursor.getString(cursor.getColumnIndex("CJR")));
							jbxxb.setCjrsfz(cursor.getString(cursor.getColumnIndex("CJRSFZ")));
							jbxxb.setUuid(cursor.getString(cursor.getColumnIndex("UUID")));
							jbxxb.setBbh(cursor.getInt(cursor.getColumnIndex("BBH")));
							jbxxb.setSjzt(cursor.getInt(cursor.getColumnIndex("SJZT")));
							jbxxb.setCjsj(new Date(cursor.getLong(cursor.getColumnIndex("CJSJ"))));
							mList.add(jbxxb);
						}while (cursor.moveToNext());
					}
					cursor.close();
				}catch (DaoException e){
					cursor.close();
					e.printStackTrace();
				}
				if (mList == null){
					arg0.onError(null);
				} else {
					arg0.onNext(mList);
					arg0.onCompleted();
				}
			}
		});
	}
	
	/**
	 * 查询登录人的所有采集记录
	 * @param
	 * @return
	 */
	public Observable<List<TAppBfrjJbxxb>> getAllCjkhxx(String zh, String value, Date date){
		final String val = value;
		final Date mDate = date;
		final String mZh = zh;
		return Observable.create(new Observable.OnSubscribe<List<TAppBfrjJbxxb>>() {

			@Override
			public void call(Subscriber<? super List<TAppBfrjJbxxb>> arg0) {
				// TODO Auto-generated method stub
				TAppBfrjJbxxbDao dao = BankApplication.getDaoSession().getTAppBfrjJbxxbDao();
				QueryBuilder<TAppBfrjJbxxb> qb = dao.queryBuilder();
				TAppBfrjJtcybDao dao1 = BankApplication.getDaoSession().getTAppBfrjJtcybDao();
				Date sDate = DateTimeUtil.strToDate("2016-01-01 00:00:00");
				QueryBuilder<TAppBfrjJtcyb> qd1 = dao1.queryBuilder();
				Date eDate = new Date(mDate.getTime() - 1000);

				WhereCondition dateWhereCondition = TAppBfrjJbxxbDao.Properties.Cjsj.between(sDate, eDate);
				qb.where(dateWhereCondition, TAppBfrjJbxxbDao.Properties.Cjrsfz.eq(mZh), TAppBfrjJbxxbDao.Properties.Sjzt.notEq(AppUtil.sjzt_delete),
						qb.or(TAppBfrjJbxxbDao.Properties.Zjhm.like("%"+val+"%"), TAppBfrjJbxxbDao.Properties.Khxm.like("%"+val+"%"))).orderDesc(TAppBfrjJbxxbDao.Properties.Cjsj);
				
				//List<TAppBfrjJbxxb> mList = qb.limit(10).list();
				List<TAppBfrjJbxxb> mList = qb.list();
				if (mList == null){
					arg0.onError(null);
				} else {
					arg0.onNext(mList);
					arg0.onCompleted();
				}
			}
		});
	}

	/**
	 * 根据uuid删除
	 * @param uuid
	 * @return
	 */
	public Observable<String> delete(final String uuid) {
		return Observable.create(new Observable.OnSubscribe<String>() {

			@Override
			public void call(final Subscriber<? super String> arg0) {
				// TODO Auto-generated method stub
				final DaoSession session = BankApplication.getDaoSession();
				session.runInTx(new Runnable() {
					@Override
					public void run() {
						// TODO Auto-generated method stub
						TAppBfrjJbxxbDao dao = session.getTAppBfrjJbxxbDao();
						QueryBuilder<TAppBfrjJbxxb> qb = dao.queryBuilder();
						qb.where(TAppBfrjJbxxbDao.Properties.Uuid.eq(uuid));
						List<TAppBfrjJbxxb> mList = qb.list();
						
						try{
							dao.deleteInTx(mList);
							arg0.onNext(uuid);
							arg0.onCompleted();
						}catch(Exception e){
							arg0.onError(e);
						}
					}
				});
			}
		});
	}

	/**
	 * 更新状态
	 * @param jcuuid
	 * @return
	 */
	public static Observable<String> delCjkhJbxxZt(String jcuuid) {

		return Observable.create(new Observable.OnSubscribe<String>() {

			@Override
			public void call(final Subscriber<? super String> arg0) {
				// TODO Auto-generated method stub
				final DaoSession session = BankApplication.getDaoSession();
				session.runInTx(new Runnable() {
					@Override
					public void run() {
						DaoSession session = BankApplication.getDaoSession();
						TAppBfrjJbxxbDao dao = session.getTAppBfrjJbxxbDao();
						QueryBuilder<TAppBfrjJbxxb> khxxqb = dao.queryBuilder();
						khxxqb.where(TAppBfrjJbxxbDao.Properties.Uuid.eq(jcuuid));
						List<TAppBfrjJbxxb> mList = khxxqb.list();
						for (TAppBfrjJbxxb mTAppBfrjJbxxb : mList) {
							mTAppBfrjJbxxb.setSjzt(AppUtil.sjzt_delete);
							dao.update(mTAppBfrjJbxxb);
						}
						try{
							arg0.onNext(jcuuid);
							arg0.onCompleted();
						}catch(Exception e){
							arg0.onError(e);
						}
					}
				});
			}
		});
	}

	//  时间: 2020/4/21 3:14 PM ,作者: xjc, 作用: 网络删除拜访记录
	public Observable<String> delBfrjInfo(String str){
		return HttpMethodServlet.getInstance().delBfrjInfo(str);
	}
}
